# encoding: utf-8
require './lib/httpclient-4.2.2.jar'
require './lib/httpcore-4.2.2.jar'
require './lib/JSON4J.jar'
require './lib/commons-logging-1.1.1.jar'

include Java
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;


import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;

class FetchBskkPage
  $count = 0 #记录有效帖子数目
  $loop_count = 0
  
 
  
  def fetch(args)
    
    threads = Array.new
    
    p "# =>" +  "Version : 130503" 
      
    df = SimpleDateFormat.new("yyyy-MM-dd hh:mm:ss")
    
    p "# =>" +  "start time: " + "#{df.format(Date.new) }"  
    
    if (args == nil ||args.length < 2)
      p "Please input start pageid and end pageid !"
      return
    end
    
    #startpageid = 136150    endstartpageid = 2827929
    startpageid = args[0]
    endpageid = args[1]
    
    p "# =>" +  "Start Fetch Page : " + "#{startpageid}"
      
    begin
      for i in startpageid..endpageid do 
        threads << Thread.start{
#          p i.to_s
          getJSONBskkPage(startpageid.to_s)
        }
      end
      
    rescue
        p "fetch error:#{$!} at:#{$@}"
    end

      threads.each{|t| t.join}
    p "# =>" +  "end time: " + "#{df.format(Date.new) }"   
  end

  def getJSONBskkPage(uid)
    surl = ""

    pagings = JSONArray.new
    
    jpage = JSONObject.new
    
    flag = false
    
    httpclient = DefaultHttpClient.new

    begin
      sUrl = "http://bskk.com/forum.php?mod=viewthread&tid=" + uid.to_s
     # p "current url: " + sUrl
      
      
      
      [1..200].each do |i|
        
        paging = JSONObject.new
        #Create an instance of HttpGet.
        httpget = HttpGet.new(sUrl + "&extra=&page=" + i.to_s)
       #Create a response handler
        responsehandler = BasicResponseHandler.new          
        responseBody = httpclient.execute(httpget,responsehandler);
        
        if(!responseBody.include?"下一页")  
         paging.put("page"+ i.to_s, responseBody)
         pagings.add(paging)
         break
        end
         
        paging.put("page"+ i.to_s, responseBody)     
        pagings.add(paging)
        
      end
    rescue 
      p "#=> " +  "Error page url: " + sUrl
      p "get JSONBskkPage error:#{$!} at:#{$@}"
    ensure
      httpclient.getConnectionManager().shutdown()
    end

    jpage.put("surl", sUrl)
    jpage.put("body", pagings)

  # p "jpage #=>" + jpage.to_s if !jpage.nil?
  if(!isIncludeKeyWord(pagings,"日日慈"))#过滤其他帖子，只保留日日慈帖子
     jpage = nil
   end
   
   jpage
  end

  def isIncludeKeyWord(pagings,keyword)
    flag = false
    if (pagings != nil && pagings.size() > 0)
      for i in 0..pagings.size-1 do
        pagenum = i+1
        paging = pagings.get(i)
        p paging.get("page" + pagenum.to_s).to_s
        if paging.get("page" + pagenum.to_s).to_s.include?keyword
          flag = true
          break
        end
          
      end
        
     end
      flag
   end
  
  def saveJSONPageFile(jpage)
    writer = nil
    filePath = "./file/pages.txt"
    
    begin
      return  if jpage.nil?
      
      writer = BufferedWriter.new(FileWriter.new(File.new(filePath),true))
      writer.write(jpage.toString())
      writer.newLine()
      writer.flush()
      writer.close()
      
    rescue
      p "#=> " +  "Error page url: " + jpage.get("surl").to_s
      p "save JSON Page File error:#{$!} at:#{$@}"
    end
    p "#=> " +  "Effective page url: " + jpage.get("surl").to_s
    
   end


end


fb = FetchBskkPage.new

fb.fetch([136150,136250])